package tools.maf;


public class mafSequence {

	private String name;
	private StringBuffer sequence,quality;
	private char strand;
	private int start,alignmentLength,totLength;
	private boolean hasQuality;
	
	protected mafSequence(){
		super();
		hasQuality=false;
		sequence=new StringBuffer();
		quality=new StringBuffer();
	}
	
	protected mafSequence(String sLine){
		this();
		this.addSequenceLine(sLine);
	}
	
	protected mafSequence(mafSequence b){
		this.name=b.name;
		this.sequence=new StringBuffer(b.sequence);
		this.quality=new StringBuffer(b.quality);
		this.strand=b.strand;
		this.start=b.start;
		this.alignmentLength=b.alignmentLength;
		this.totLength=b.totLength;
		this.hasQuality=b.hasQuality;
	}
	
	protected boolean addSequenceLine(String sLine){
		String[] l= sLine.split("\\s+");
		name=l[1];
		start=Integer.parseInt(l[2]);
		alignmentLength=Integer.parseInt(l[3]);
		strand=l[4].charAt(0);
		if(l[4].length()>1){
			System.err.println("line:");
			System.err.println(sLine);
			System.err.println("has too long strand field");
		}
		totLength= Integer.parseInt(l[5]);
		sequence= new StringBuffer(l[6]);
		return true;
	}
	
	protected boolean addQualityLine(String qLine){
		String[] l= qLine.split("\\s+");
		if(!l[1].equals(name) || l[2].length()!= sequence.length()){
			return false;
		}
		
		quality= new StringBuffer(l[2]);
		hasQuality=true;
		return true;
	}
	
	public String toString(){
		StringBuffer s=new StringBuffer(" ");
		s.append(start);
		s.append(' ');
		s.append(alignmentLength);
		s.append(' ');
		s.append(strand);
		s.append(' ');
		s.append(totLength);
		s.append(' ');
		int size=s.length();
		s.insert(0, name);
		s.insert(0, "s ");
		s.append(sequence);
		if(hasQuality){
			s.append("\nq ");
			s.append(name);
			for(int i=0;i<size;++i){
				s.append(' ');
			}
			s.append(quality);
		}
		return s.toString();
	}

	protected boolean hasQuality() {
		return hasQuality;
	}

	protected String getName() {
		return name;
	}

	protected String getSequence() {
		return sequence.toString();
	}

	protected String getQuality() {
		return quality.toString();
	}

	protected char getStrand() {
		return strand;
	}

	protected int getStart() {
		return start;
	}

	protected int getAlignmentLength() {
		return alignmentLength;
	}

	protected int getTotLength() {
		return totLength;
	}
	
	protected int trimRight(){
		final int lastIndex=sequence.length()-1;
		int trim=0;
		if(lastIndex<0){
			return -1;
		}
		if(sequence.charAt(lastIndex)!='-'){
			trim=1;
			--alignmentLength;
		}
		sequence.deleteCharAt(lastIndex);
		if(hasQuality()){
			quality.deleteCharAt(lastIndex);
		}
		return trim;
	}
	
	protected int trimLeft(){
		int trim=0;
		if(sequence.length()==0){
			return -1;
		}
		if(sequence.charAt(0)!='-'){
			trim=1;
			--alignmentLength;
			++start;
		}
		sequence.deleteCharAt(0);
		if(hasQuality()){
			quality.deleteCharAt(0);
		}
		return trim;
	}
	
	protected boolean gapRight(){
		final int lastIndex=sequence.length()-1;
		return sequence.charAt(lastIndex)=='-';
	}
	
	protected boolean gapLeft(){
		return sequence.charAt(0)=='-';
	}
	
//	protected void trimRight(int size)throws Exception{
//		if(size>=alignmentLength){
//			throw new Exception("trying to trim "+size+" bp from "+name+", which only have "+alignmentLength+" bp aligned");
//		}
//		alignmentLength=alignmentLength-size;
//		sequence=sequence.substring(0, sequence.length()-size);
//		if(hasQuality()){
//			quality=quality.substring(0, quality.length()-size);
//		}
//	}
	
//	protected void trimLeft(int size)throws Exception{
//		if(size>=alignmentLength){
//			throw new Exception("trying to trim "+size+" bp from "+name+", which only have "+alignmentLength+" bp aligned");
//		}
//		
//		alignmentLength=alignmentLength-size;
//		start=start+size;
//		sequence=sequence.substring(size);
//		if(hasQuality()){
//			quality=quality.substring(size);
//		}
//	}
	
	
}
